package com.baiyang.app.ui.bookshelf.workList;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class WorkListService {

    private Context context;
    private WorkListViewModel viewModel;
    public WorkListService(Context context,WorkListViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }


    /**
     * 根据作者ID获取图书信息
     * @param authorId
     */
    public void getArticle(String authorId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("authorId",authorId);
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    viewModel.setArticleResult(JSONObject.parseObject(str));
                    LogUtils.i("getArticleByAuthor",str);
                }else{
                    LogUtils.i("getArticleByAuthor","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("getArticleByAuthor","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.Article.GET_ARTICLE_BY_AUTHOR,jsonObject);
    }
}
